package com.yangj.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import org.apache.commons.dbutils.ResultSetHandler;
import com.yangj.model.AccompanyingPlay;
import com.yangj.model.Game;
import com.yangj.model.ServiceType;
public class AccompanyingPlayDao extends BaseDao{
	public List<AccompanyingPlay> getAll(int gameId) {
		try {
			String sql="select accompanying_play.id aid, "
					+ " accompanying_play.name aname, level_, "
					+ " order_count, loc, acc_img, message, "
					+ " price, dan, game_id, game.name gname "
					+ " from accompanying_play"
					+ " join game on game_id = game.id "
					+ " and game_id = ?"
					+ " order by level_ ,order_count desc";
			return queryRunner.query(sql,new ResultSetHandler<List<AccompanyingPlay>>() {
				@Override
				public List<AccompanyingPlay> handle(ResultSet rs) throws SQLException {
					List<AccompanyingPlay> list=new LinkedList<>();
					while(rs.next()) {
						list.add(new AccompanyingPlay(
								rs.getInt("aid"),
								rs.getString("aname"),
								rs.getDouble("level_"),
								rs.getInt("order_count"),
								rs.getString("loc"),
								rs.getString("acc_img"),
								rs.getString("message"),
								rs.getInt("price"),
								rs.getString("dan"),
								new Game(rs.getInt("game_id"),
										rs.getString("gname"))
								));
					}
					return list;
				}
			},gameId);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public List<AccompanyingPlay> getAll() {
		try {
			String sql="select accompanying_play.id aid, accompanying_play.name aname, level_, "
					+ " order_count, loc, acc_img, message, price, dan, game_id, game.name gname "
					+ " from accompanying_play"
					+ " join game on game_id = game.id "
					+ " order by level_ desc,order_count desc"
					;
			return queryRunner.query(sql,new ResultSetHandler<List<AccompanyingPlay>>() {
				@Override
				public List<AccompanyingPlay> handle(ResultSet rs) throws SQLException {
					List<AccompanyingPlay> list=new LinkedList<>();
					while(rs.next()) {
						list.add(new AccompanyingPlay(rs.getInt("aid"),
								rs.getString("aname"),
								rs.getDouble("level_"),
								rs.getInt("order_count"),
								rs.getString("loc"),
								rs.getString("acc_img"),
								rs.getString("message"),
								rs.getInt("price"),
								rs.getString("dan"),
								new Game(rs.getInt("game_id"),
										rs.getString("gname"))
								));
					}
					return list;
				}
			});
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
}
